﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using Intelligent.OTC.Domain.DataModel;
using Intelligent.OTC.Common.UnitOfWork;
using Intelligent.OTC.Domain;
using Intelligent.OTC.Business;
using Intelligent.OTC.Domain.Repositories;
using Intelligent.OTC.Common;
using System.Configuration;
using System.IO;
using Intelligent.OTC.Common.Utils;
using Intelligent.OTC.WebApi.Core;
using Intelligent.OTC.Common.Attr;

namespace Intelligent.OTC.WebApi.Controllers
{
    [UserAuthorizeFilter(actionSet: "common")]
    public class InvoiceController : ApiController
    {
        private string strDeal = AppContext.Current.User.Deal.ToString();
        // Get CustomerAging Single
        [HttpGet]
        public CustomerDetail Get(int id)
        {
            CustomerService service = SpringFactory.GetObjectImpl<CustomerService>("CustomerService");
            CustomerDetail cust =  service.GetCustomerDetail(id);
            return cust;
        }

        [HttpGet]
        public List<InvoiceAging> GetBy(string num)
        {
            InvoiceService service = SpringFactory.GetObjectImpl<InvoiceService>("InvoiceService");
            return service.invoiceInfoGetByNum(num);
        }


        [HttpPut]
        public void Put([FromBody]InvoiceAging invoice)
        {
            InvoiceService service = SpringFactory.GetObjectImpl<InvoiceService>("InvoiceService");
            InvoiceAging old = service.CommonRep.FindBy<InvoiceAging>(invoice.Id);
            ObjectHelper.CopyObjectWithUnNeed(invoice, old, new string[] { "Id" });
            service.CommonRep.Commit();
        }

        #region GetInvoiceList
        [HttpGet]
        [PagingQueryable]
        public IEnumerable<InvoiceAging> Get()
        {
            InvoiceService service = SpringFactory.GetObjectImpl<InvoiceService>("InvoiceService");
            return service.invoiceInfoGet().AsQueryable<InvoiceAging>();
        }
        #endregion

        [HttpGet]
        public IEnumerable<InvoiceAging> Get(string num ,string site)
        {
            InvoiceService service = SpringFactory.GetObjectImpl<InvoiceService>("InvoiceService");
            List<InvoiceAging> invoice = new List<InvoiceAging>();
            invoice = service.invoiceInfoGet().ToList<InvoiceAging>();
            invoice = invoice.FindAll(m => m.CustomerNum == num && m.Deal == strDeal && m.LegalEntity == site);

            return invoice.AsQueryable<InvoiceAging>();
        }

        [HttpGet]
        public IEnumerable<InvoiceAging> Get(string num, string site, string act,string cls)
        {
            if (act == "collectorsoa")
            {
                InvoiceService service = SpringFactory.GetObjectImpl<InvoiceService>("InvoiceService");
                List<InvoiceAging> invoice = new List<InvoiceAging>();
                invoice = service.invoiceInfoGet().ToList<InvoiceAging>();
                invoice = invoice.FindAll(m => m.CustomerNum == num && m.Deal == strDeal && m.LegalEntity == site && m.Class == cls);

                return invoice.AsQueryable<InvoiceAging>();
            }
            else {
                List<InvoiceAging> invoice = new List<InvoiceAging>();
                return invoice;
            }
        }

        [HttpGet]
        public void UpdateInvoiceState(string invids, string status, string act)
        {
            if (act == "updatestatus")
            {
                InvoiceService service = SpringFactory.GetObjectImpl<InvoiceService>("InvoiceService");
                
                string[] arr = invids.Split(',');
                foreach (var item in arr)
                {
                    InvoiceAging old = service.CommonRep.FindBy<InvoiceAging>(Convert.ToInt32(item));
                    InvoiceAging invoice = old;
                    invoice.States = status;
                    ObjectHelper.CopyObjectWithUnNeed(invoice, old, new string[] { "Id" });
                }
                service.CommonRep.Commit();
            }
        }
    }
}